package com.xywh.composeLibrary.view

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Button
import androidx.compose.material.Divider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

/**
 *
 * @param dataList List<T> 列表数据
 * @param loadingState StateEnum 列表页面的状态
 * @param boxModifier Modifier 修饰器
 * @param reloadOnClick Function0<Unit> 如果数据为空，错误状态时，点击事件
 * @param listItem [@androidx.compose.runtime.Composable] Function2<T, [@kotlin.ParameterName] Int, Unit> 列表数据item布局
 */
@Composable
fun <T> XxMessageList(
    dataList: List<T>,
    loadingState: StateEnum = StateEnum.EMPTY,
    boxModifier: Modifier = Modifier.padding(20.dp, 0.dp),
    reloadOnClick: () -> Unit,
    listItem: @Composable (T, index: Int) -> Unit
) {

    if (dataList.isNotEmpty()) {
        XxLoadSir(stateEnum = loadingState, reloadOnClick) {
            XxMessageList(
                listMessage = dataList, listItem = listItem, boxModifier = boxModifier
            )
        }
    } else {
        XxLoadSir(stateEnum = StateEnum.EMPTY, reloadOnClick) {
            XxMessageList(
                listMessage = dataList, listItem = listItem, boxModifier = boxModifier
            )
        }
    }
}

/**
 *
 * @param listMessage List<T> 列表数据
 * @param boxModifier Modifier 修饰器
 * @param listItem [@androidx.compose.runtime.Composable] Function2<T, [@kotlin.ParameterName] Int, Unit> 列表数据item布局
 */
@Composable
private fun <T> XxMessageList(
    listMessage: List<T>,
    boxModifier: Modifier,
    listItem: @Composable (T, index: Int) -> Unit
) {
    Box(boxModifier) {

        val listState = rememberLazyListState()
        val coroutineScope = rememberCoroutineScope()

        LazyColumn(state = listState) {

            items(
                count = listMessage.size,
                key = { index ->
                    "$index"
                }) { index ->
                listItem(listMessage[index], index)
                Divider()
            }

        }

        val showButton by remember {
            derivedStateOf {
                listState.firstVisibleItemIndex > 0
            }
        }

        AnimatedVisibility(visible = showButton) {
            Button(
                onClick = {
                    coroutineScope.launch {
                        // Animate scroll to the first item
                        listState.animateScrollToItem(index = 0)
                    }
                }
            ) {

                Text(text = "asdas")
            }
        }
    }
}

